package implementations;

import interfaces.ConnectionSource;
import interfaces.PoolFactory;

import java.util.concurrent.TimeUnit;

/**
 * Creational pattern for pool
 * @param <T>
 */
public class MyPoolFactory<T> implements PoolFactory<T>{

    /**
     * Factory that returns simple pool only with max capacity and no
     * keep alive delay
     * @param capacity maximum amount of concurrent connections in pool
     * @param src resource to use
     * @return new instance of concurrent pool
     */
    @Override
    public MyConnectionPool<T> newConnectionPool(int capacity, ConnectionSource<T> src) {
        return new MyConnectionPool<>(capacity, capacity, Long.MAX_VALUE, TimeUnit.MILLISECONDS, src);
    }

    /**
     * Factory that provides maximum flexibility
     * @param min minimum alive connections
     * @param max maximum concurrent connections
     * @param keepAliveTime after that delay connections, that
     *                      are not used will be deleted
     * @param unit time unit for delay
     * @param src resource to use
     * @return new instance of concurrent pool
     */
    @Override
    public MyConnectionPool<T> newConnectionPool(int min, int max, long keepAliveTime, TimeUnit unit, ConnectionSource<T> src) {
        return new MyConnectionPool<>(min, max, keepAliveTime, unit, src);
    }
}
